home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / GNUSUTIL.ZIP / src / basename.c next >
Encoding:
C/C++ Source or Header  |  1994-12-17  |  2.6 KB  |  113 lines

  1. /* basename -- strip directory and suffix from filenames
  2.    Copyright (C) 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: basename name [suffix]
  19.    NAME is a pathname; SUFFIX is a suffix to strip from it.
  20.  
  21.    basename /usr/foo/lossage/functions.l
  22.    => functions.l
  23.    basename /usr/foo/lossage/functions.l .l
  24.    => functions
  25.    basename functions.lisp p
  26.    => functions.lis */
  27.  
  28. #include <config.h>
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31.  
  32. #include "system.h"
  33. #include "version.h"
  34. #include "long-options.h"
  35.  
  36. char *basename ();
  37. void error ();
  38. void strip_trailing_slashes ();
  39.  
  40. static void remove_suffix ();
  41.  
  42. /* The name this program was run with. */
  43. char *program_name;
  44.  
  45. static void
  46. usage (status)
  47.      int status;
  48. {
  49.   if (status != 0)
  50.     fprintf (stderr, "Try `%s --help' for more information.\n",
  51.          program_name);
  52.   else
  53.     {
  54.       print_version("basename");
  55.       printf ("\
  56. Usage: %s PATH [SUFFIX]\n\
  57.   or:  %s OPTION\n\
  58. ",
  59.           program_name, program_name);
  60.       printf ("\
  61. \n\
  62.   --help      display this help and exit\n\
  63.   --version   output version information and exit\n\
  64. ");
  65.     }
  66.   exit (status);
  67. }
  68.  
  69. void
  70. main (argc, argv)
  71.      int argc;
  72.      char **argv;
  73. {
  74.   char *name;
  75.  
  76.   program_name = argv[0];
  77.  
  78.   parse_long_options (argc, argv, "basename", version_string, usage);
  79.  
  80.   if (argc == 1 || argc > 3)
  81.     usage (0);
  82.  
  83.   strip_trailing_slashes (argv[1]);
  84.  
  85.   name = basename (argv[1]);
  86.  
  87.   if (argc == 3)
  88.     remove_suffix (name, argv[2]);
  89.  
  90.   puts (name);
  91.  
  92.   exit (0);
  93. }
  94.  
  95. /* Remove SUFFIX from the end of NAME if it is there, unless NAME
  96.    consists entirely of SUFFIX. */
  97.  
  98. static void
  99. remove_suffix (name, suffix)
  100.      register char *name, *suffix;
  101. {
  102.   register char *np, *sp;
  103.  
  104.   np = name + strlen (name);
  105.   sp = suffix + strlen (suffix);
  106.  
  107.   while (np > name && sp > suffix)
  108.     if (*--np != *--sp)
  109.       return;
  110.   if (np > name)
  111.     *np = '\0';
  112. }
  113.